package cn.jiaqiang.chatgpt.data.domain.openai.service.chat.userType;

import cn.jiaqiang.chatgpt.data.domain.openai.model.aggregates.ChatProcessAggregate;
import cn.jiaqiang.chatgpt.data.domain.openai.model.entity.RuleLogicEntity;
import cn.jiaqiang.chatgpt.data.domain.openai.model.entity.UserAccountEntity;
import cn.jiaqiang.chatgpt.data.domain.openai.model.valobj.LogicCheckTypeVO;
import cn.jiaqiang.chatgpt.data.domain.openai.service.channel.OpenAiGroupService;
import cn.jiaqiang.chatgpt.data.domain.openai.service.channel.impl.ChatGLMService;
import cn.jiaqiang.chatgpt.data.domain.openai.service.channel.impl.ChatGPTService;
import cn.jiaqiang.chatgpt.data.domain.openai.service.chat.IUserTypeGroupService;
import cn.jiaqiang.chatgpt.data.domain.openai.service.chat.UserChatService;
import cn.jiaqiang.chatgpt.data.domain.openai.service.rule.factory.DefaultLogicFactory;
import cn.jiaqiang.chatgpt.data.types.enums.OpenAiChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

/**
 * User: jia-qiang
 * Date: 2024-04-04
 * Time: 21:47
 */
@Component
public class NormalUserUserChatService extends UserChatService implements IUserTypeGroupService {

    private static final Logger log = LoggerFactory.getLogger(NormalUserUserChatService.class);

    public NormalUserUserChatService(ChatGPTService chatGPTService, ChatGLMService chatGLMService) {
        super(chatGPTService, chatGLMService);
    }

    @Override
    public boolean chat(ChatProcessAggregate chatProcess, UserAccountEntity userAccountEntity, ResponseBodyEmitter emitter) throws Exception {
        // 3. 规则过滤
        RuleLogicEntity<ChatProcessAggregate> ruleLogicEntity = this.doCheckLogic(
                chatProcess, userAccountEntity,
                DefaultLogicFactory.LogicModel.ACCESS_LIMIT.getCode(), // 访问频次
                DefaultLogicFactory.LogicModel.SENSITIVE_WORD.getCode(),  // 敏感词
                userAccountEntity == null || userAccountEntity.getSurplusQuota() == 0 ? null : DefaultLogicFactory.LogicModel.ACCOUNT_STATUS.getCode(), // 用户状态
                DefaultLogicFactory.LogicModel.MODEL_TYPE.getCode(), // 模型类型
                userAccountEntity == null || userAccountEntity.getSurplusQuota() == 0 ? null : DefaultLogicFactory.LogicModel.USER_QUOTA.getCode() // 用户额度
        );
        log.info("用户 userId = {} 校验结果: {}", chatProcess.getUserId(), ruleLogicEntity.getType());
        if (!LogicCheckTypeVO.SUCCESS.equals(ruleLogicEntity.getType())) {
            emitter.send(ruleLogicEntity.getInfo());
            emitter.complete();
            throw new RuntimeException("校验未通过");
        }
        // 4. 应答处理 【ChatGPT、ChatGLM 策略模式】
        try {
            OpenAiChannel channel = chatProcess.getChannel();
            OpenAiGroupService openAiGroupService = openAiGroup.get(channel);
            openAiGroupService.doMessageResponse(ruleLogicEntity.getData(), emitter);
            return true;
        } catch (Exception e) {
            throw new RuntimeException("流式对话失败, ", e);
        }
    }
}
